import cgi
from google.appengine.api import users
from google.appengine.ext import webapp

class PortfolioSettingsService(webapp.RequestHandler):
	def get(self):
		if self.request.get('action')=='createportfolio':
			portfoliosettingsguid = self.request.get('portfoliosettingsguid')
			self.response.out.write(self.createportfolio(portfoliosettingsguid))
		elif self.request.get('action')=='getuseremailaddress':
			self.response.out.write(self.getUserEmailAddress())
		elif self.request.get('action')=='getusernickname':
			self.response.out.write(self.getUserNickname())
		elif self.request.get('action')=='getloginhtml':
			if self.request.get('redirect'):
				redirect = cgi.decode(self.request.get('redirect'))
				self.response.out.write(self.getloginhtml(redirect))
			else:
				self.response.out.write(self.getloginhtml())
		elif self.request.get('action')=='getlogouthtml':
			self.response.out.write(self.getlogouthtml())
		else:
			self.response.out.write('unknown request')
	
	def isloggedin(self):
		user = users.get_current_user()
		if user:
			return 'true'
		else:
			return 'false'
			
	def getloginhtml(self, redirect='/'):
		return ("<a href=\"%s\">Sign in or register</a>." %
		users.create_login_url( redirect))
	
	def getlogouthtml(self):
		return ("Welcome, %s! (<a href=\"%s\">sign out)" %
		(users.get_current_user().nickname(), users.create_logout_url("/")))
		
	def getUserEmailAddress(self):
		return users.get_current_user().email()
		
	def getUserNickname(self):
		return users.get_current_user().nickname()
		
